package com.neuedu.hadoop.wordcount;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
/*
 四个泛型解释:
 KEYIN: K2类型
 VALULEIN: V2类型
 KEYOUT: K3类型
 VALUEOUT:V3类型
 */
public class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
    // reduce⽅法作⽤: 将新的K2和V2转为 K3和V3 ，将K3和V3写⼊上下⽂中
 /*
 参数:
 key ： 新K2
 values： 集合 新 V2
 context ：表示上下⽂对象
 ----------------------
 如何将新的K2和V2转为 K3和V3
 新 K2 V2
 hello <1,1,1>
 world <1,1>
 hadoop <1>
 ------------------------
 K3 V3
 hello 3
 world 2
 hadoop 1
 */
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        //1:遍历集合，将集合中的数字相加，得到 V3
        long cnt = 0L;
        LongWritable longWritable = new LongWritable();
        for (LongWritable value : values){
            //2:将K3和V3写⼊上下⽂中
            cnt += value.get();
        }
        longWritable.set(cnt);
        context.write(key, longWritable);
    }

}